Event.observe ( window, 'load', bootstrap );

function bootstrap () {
  $( 'product'        ).on ( 'change', product_changed        );
  $( 'no_description' ).on ( 'change', no_description_changed );
  
  var parameter = window.location.search.toQueryParams ();
  
  if ( parameter['product_id'] ) {
    $( 'product' ).value = parameter['product_id'];
  }
  
  if ( $( 'product' ).value != '' ) {
    product_changed ();
  }
}

function product_changed () {
  var product = $( 'product' ).value;
  
  if ( product != '' ) {
    display_progress ( 'Querying tracker...' );
    
    new Ajax.Request ( '/rest/product/' + product + '/datasheets', {
      method         : 'get',
      requestHeaders : { Accept : 'application/json' },
      onSuccess      : function ( response ) {
        var datasheets = response.responseJSON;

        if ( datasheets.length > 0 ) {
          $( 'datasheets_table_wrapper' ).replace ( datasheets_table ( datasheets ) );
                    
          no_description_changed ();
        }
        else {
          $( 'datasheets_table' ).replace ( '<div id="datasheets_table_wrapper"><p class="error">No datasheets!</p></div>' );
        }
      },
      onComplete     : function () {
        hide_progress ();
      }
    } );
  }
}

function datasheets_table ( datasheets ) {
  var has_resolution;
  
  datasheets.each ( function ( h0 ) {
    if ( h0.issues.length ) {
      h0.issues.each ( function ( h ) {
        if ( h.resolution ) {
          has_resolution = 1;
        }
      } );
    }
  } );
  
  var html =
    "<div id=\"datasheets_table_wrapper\">\n"
  + "  <h2>Datasheets</h2>\n"
  + "  <table id=\"datasheets_table\" class=\"datasheets\">\n"
  + "    <thead>\n"
  + "      <tr>\n"
  + "        <th>ID</th>\n"
  + "        <th>Version</th>\n"
  + "        <th>Issue</th>\n"
  + "        <th>Requestor</th>\n"
  + "        <th>Created on</th>\n"
  + "        <th>Closed on</th>\n"
  + "        <th>Status</th>\n"
  + ( has_resolution ?
    "        <th>Resolution</th>\n" : "" )
  + "        <th>Owner</th>\n"
  + "        <th>Summary<span class=\"with_description\"> + Description</span></th>\n"
  + "      </tr>\n"
  + "    </thead>\n"
  + "    <tfoot>\n"
  + "      <tr>\n"
  + "        <th>ID</th>\n"
  + "        <th>Version</th>\n"
  + "        <th>Issue</th>\n"
  + "        <th>Requestor</th>\n"
  + "        <th>Created on</th>\n"
  + "        <th>Closed on</th>\n"
  + "        <th>Status</th>\n"
  + ( has_resolution ?
    "        <th>Resolution</th>\n" : "" )
  + "        <th>Owner</th>\n"
  + "        <th>Summary</th>\n"
  + "      </tr>\n"
  + "    </tfoot>\n"
  + "    <tbody>\n"
  ;

  var i = 1;

  datasheets.each ( function ( h0 ) {
    var rowspan = h0.issues.length;

    var th_class =
      ( h0.svn_ahead && ( h0.version == h0.svn_last ) ) ?
        " class=\"ahead\" title=\"trunk is ahead" + ( h0.svn_last ? " of " + h0.svn_last + " last tag" : "" ) + "\""
    : (
      ( h0.svn_last  && ( h0.version != h0.svn_last ) ) ?
        " class=\"" + ( h0.version < h0.svn_last ? "behind" : "ahead" ) + "\" title=\"" + ( h0.version < h0.svn_last ? "behind" : "ahead" ) + " " + h0.svn_last + " last tag\""
    :
        ""
    );

    var a = h0.version != 0 ? "<a href=\"/datasheet/issues?id=" + h0.id + "&tag=" + h0.version + "\">" + h0.version + "</a>" : "-";

    if ( rowspan ) {
      h0.issues.each ( function ( h ) {
        html = html
        + "      <tr class=\"" + ( i % 2 ? "odd" : "even" ) + "\">\n"
        + ( rowspan ?
          "        <th title=\"" + h0.description + "\" rowspan=\"" + rowspan + "\">" + ( h0.url ? "<a href=\"" + h0.url + "\">" : "" ) + ( h0.id ) + ( h0.url ? "</a>" : "" ) + "</th>\n"
        + "        <th rowspan=\"" + rowspan + "\"" + th_class + ">"                  + a                                                                                      + "</th>\n"
        : ""
        )
        + "        <td><a href=\"" + h.issue_url + "\">"                          +   h.issue_id            + "</a></td>\n"
        + "        <td>"                                                          +   h.requestor           +     "</td>\n"
        + "        <td>"                                                          +   h.created_on          +     "</td>\n"
        + "        <td>"                                                          + ( h.closed_on  || "-" ) +     "</td>\n"
        + "        <td" + ( h.status ? " class=\"" + h.status.classify () + "\">" +   h.status : ">-" )     +     "</td>\n"
        + ( has_resolution ?
          "        <td>"                                                          + ( h.resolution || '-' ) +     "</td>\n" : "" )
        + "        <td>"                                                          +   h.owner               +     "</td>\n"
        + "        <td>\n"
        + "          <p class=\"summary\" title=\"" + h.description + "\">" + h.summary + "</p>\n"
        + "          <textarea class=\"description with_description\" readonly=\"readonly\">" + h.description + "</textarea></td>\n"
        + "        </td>\n"
        + "      </tr>\n"
        ;

        rowspan = 0;
        i       = i + 1;
      } );
    }
    else {
      html = html
      + "      <tr class=\"" + ( i % 2 ? "odd" : "even" ) + "\">\n"
      + "        <th title=\"" + h0.description + "\">" + ( h0.url ? "<a href=\"" + h0.url + "\">" : "" ) + ( h0.id ) + ( h0.url ? "</a>" : "" ) + "</th>\n"
      + "        <th" + th_class + ">"                  + a                                                                                      + "</th>\n"
      + "        <td>-</td>\n"
      + "        <td>-</td>\n"
      + "        <td>-</td>\n"
      + "        <td>-</td>\n"
      + ( has_resolution ?
        "        <td>-</td>\n" : "" )
      + "        <td>-</td>\n"
      + "        <td>-</td>\n"
      + "        <td>-</td>\n"
      + "      </tr>\n"
      ;

      i = i + 1;
    }
  } );

  html = html
  + "    </tbody>\n"
  + "  </table>\n"
  + "  </div>\n"
  ;
  
  return html;
}
